<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\CartsRepository;
use App\Repository\Models\Carts;

/**
 * Class CartsRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class CartsRepositoryEloquent extends BaseRepository implements CartsRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'product',
        'sku'
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Carts::class;
    }

    public function updateOrCreateCart($data)
    {
        $this->applyCriteria();
        $this->applyScope();

        $cart = $this->model->where(['sku_id' => $data['sku_id']])->first();
        if ($cart) {
            $ret = $cart->increment('num', $data['num']);
        } else {
            $ret = $this->model->create($data);
        }
        return $ret;
    }

    public function batchDelete(array $ids)
    {
        $this->applyCriteria();
        $this->applyScope();
        $ret = $this->model->whereIn('id', $ids)->delete();
        return $ret;
    }

    public function emptyCart()
    {
        $this->applyCriteria();
        $this->applyScope();
        $ret = $this->model->delete();
        return $ret;
    }

    public function batchDeleteBySku(array $sku_ids)
    {
        $this->applyCriteria();
        $this->applyScope();
        $ret = $this->model->whereIn('sku_id', $sku_ids)->delete();
        return $ret;
    }
}
